$(document).ready(function() {
	// $("table").tablesorter();
	initializeSelectAlls();

    //round corners
    $('#footer').corner("bottom");
});



/*
Update the miles field with data from the lap field
Will be performed on the onclick action?

NOTE: this function is ridiculously fragile and needs to be rewritten
*/
function convertLapToMiles(){
  var lap = document.forms[0].lap.value;
  if (lap){}
  else{
    document.forms[0].lap.value = 1;
    lap = 1;
  }
  var ran = document.forms[0].laps_ran.value;
  var miles = ran/lap;
  document.forms[0].entry_distance.value = miles;
}

/*
Update the miles field with data from the lap field
Will be performed on the onclick action?
*/
function convertGroupLapToMiles(){
  var lap = document.forms[0].lap.value;
  if (lap){}
  else{
    document.forms[0].lap.value = 1;
    lap = 1;
  }
  
  var year = document.forms[0].bulk_date_1i.value
  var month = document.forms[0].bulk_date_2i.value
  var day = document.forms[0].bulk_date_3i.value
  var dteDate
  thisform = document.forms[0];
  dteDate=new Date(year,month-1,day);
  if ((day!=dteDate.getDate()) || ((month-1)!=dteDate.getMonth()) || (year!=dteDate.getFullYear())){
    alert("the date entered is not a valid date, please correct")
    return false
  }
  
    

  for (i=7; i<=thisform.elements.length-2; i=i+5)
  {
    if ((thisform.elements[i].value)&&(thisform.elements[i+1].value))
    {      
      if (isNaN(thisform.elements[i].value) || isNaN(thisform.elements[i+1].value)){
        alert("One of the entries you entered is not a number")
        return false
        }
      if ((thisform.elements[i].value.to_i <= 0) || (thisform.elements[i+1].value.to_i <= 0)){
        alert("One of the entries you entered is not positive")
        return false
        }
      if (thisform.elements[i].value && thisform.elements[i+1].value){
        thisform.elements[i+3].value=(thisform.elements[i].value)/lap;
        thisform.elements[i+2].value=""+year+"-"+month+"-"+day;
      }
    }
  }
  return true
}

/*
Select All checkboxes in tables
*/
function initializeSelectAlls() {
	$("table").each(function() {
		var $thisTable = $(this);

		// 'select all' functionality for checkbox in thead
		$thisTable.find("thead :checkbox").click(function() {
			var selectAll = this.checked;
			$thisTable.find('tbody :checkbox').each(function() {
				this.checked = selectAll;
			});
		});
	});
}
